home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / misc / volume4 / apml / part03 < prev    next >
Encoding:
Internet Message Format  |  1989-02-03  |  33.2 KB

  1. Path: xanth!mcnc!gatech!ukma!cwjcc!hal!ncoast!allbery
  2. From: ljz@fxgrp.UUCP (Lloyd Zusman)
  3. Newsgroups: comp.sources.misc
  4. Subject: v04i114: Arbitrary Precision Math Library -- 3 of 5
  5. Message-ID: <8810051944.AA05722@fxgrp.fx.com>
  6. Date: 7 Oct 88 00:10:23 GMT
  7. Sender: allbery@ncoast.UUCP
  8. Reply-To: ljz@fxgrp.UUCP (Lloyd Zusman)
  9. Lines: 1187
  10. Approved: allbery@ncoast.UUCP
  11.  
  12. Posting-number: Volume 4, Issue 114
  13. Submitted-by: "Lloyd Zusman" <ljz@fxgrp.UUCP>
  14. Archive-name: apml/Part3
  15.  
  16. Enclosed you will find the Arbitrary Precision Math Library (3 of 5)
  17.  
  18. Please post this to the comp.sources.misc newsgroup.
  19.  
  20. I finally got this into good enough shape to send out to the net. To use,
  21. just unshar the 5 pieces, read the README file, possibly alter the makefiles
  22. to conform to your system's conventions, and then type 'make test'.
  23.  
  24. Good luck!
  25.  
  26. --
  27.   Lloyd Zusman                  Internet:  ljz@fx.com
  28.   Master Byte Software                  or ljz%fx.com@ames.arc.nasa.gov
  29.   Los Gatos, California                 or fxgrp!ljz@ames.arc.nasa.gov
  30.   "We take things well in hand."    uucp:  ...!ames!fxgrp!ljz
  31.   [ our Internet connection is down: use uucp or mail to the entry above it ]
  32.  
  33. #--------------------------Cut Here--------------------------
  34. #! /bin/sh
  35. # This is a shell archive.  Remove anything before the "#! /bin/sh" line,
  36. # then unpack it by saving it in a file and typing "sh file."
  37. #
  38. # Wrapped by Lloyd Zusman (ljz) at fxgrp on Wed Oct  5 12:41:51 1988
  39. #
  40. # unpacks with default permissions
  41. #
  42. # Contents : memory.c misc.c
  43. #
  44. if `test ! -s memory.c`
  45. then
  46. echo "x - memory.c"
  47. sed 's/^X//' > memory.c << '@\END_OF_FILE_memory.c'
  48. X/******************************************************************************
  49. X
  50. X    Arbitrary Precision Math Library General Public License
  51. X            (Written October 5, 1988)
  52. X
  53. X Copyright (C) 1988 Lloyd Zusman, Master Byte Software, Los
  54. X Gatos, California.  Everyone is permitted to copy and distribute
  55. X verbatim copies of this license, but changing it is not allowed.
  56. X You can also use this wording to make the terms for other programs.
  57. X
  58. X The wording of this license is based on that of the
  59. X "GNU EMACS GENERAL PUBLIC LICENSE" by Richard Stallman,
  60. X Copyright (C) 1985, 1987, 1988, version of February 11, 1988,
  61. X but since some of the text has been changed, please be sure to
  62. X READ THIS CAREFULLY!
  63. X
  64. X  This general public license is intended to give everyone the right
  65. Xto share the Arbitrary Precision Math Library (hereinafter referred to
  66. Xas the "APM Library").  To make sure that you get the rights we want
  67. Xyou to have, I need to make restrictions that forbid anyone to deny
  68. Xyou these rights or to ask you to surrender the rights.
  69. X
  70. X  Specifically, we want to make sure that you have the right to give
  71. Xaway copies of the APM Library, that you receive source code or else
  72. Xcan get it if you want it, that you can change the APM Library or use
  73. Xpieces of it in new programs, and that you know you can do these
  74. Xthings.
  75. X
  76. X  To make sure that everyone has such rights, we have to forbid you to
  77. Xdeprive anyone else of these rights.  For example, if you distribute
  78. Xcopies of the APM Library, you must give the recipients all the
  79. Xrights that you have.  You must make sure that they, too, receive or
  80. Xcan get the source code.  And you must tell them their rights.
  81. X
  82. X  Also, for our own protection, we must make certain that everyone
  83. Xfinds out that there is no warranty for the APM Library.  If the APM
  84. XLibrary is modified by someone else and passed on, we want its
  85. Xrecipients to know that what they have is not what we distributed, so
  86. Xthat any problems introduced by others will not reflect on our
  87. Xreputation.
  88. X
  89. X  Therefore we (Lloyd Zusman and Master Byte Software) make the
  90. Xfollowing terms which say what you must do to be allowed to
  91. Xdistribute or change the APM Library.
  92. X
  93. X            COPYING POLICIES
  94. X
  95. X1. You may copy and distribute verbatim copies of the APM Library
  96. Xsource code as you receive it, in any medium, provided that you
  97. Xconspicuously and appropriately publish on each copy a valid copyright
  98. Xnotice "Copyright (C) 1988 Lloyd Zusman, Master Byte Software, Los
  99. XGatos, California" (or with whatever year is appropriate); keep intact
  100. Xthe notices on all files that refer to this License Agreement and to
  101. Xthe absence of any warranty; and give any other recipients of the the
  102. XAPM Library program a copy of this License Agreement along with the
  103. Xprogram.  You may charge a distribution fee for the physical act of
  104. Xtransferring a copy.
  105. X
  106. X  2. You may modify your copy or copies of the APM Library source code or
  107. Xany portion of it, and copy and distribute such modifications under
  108. Xthe terms of Paragraph 1 above, provided that you also do the following:
  109. X
  110. X    a) cause the modified files to carry prominent notices stating
  111. X    that you changed the files and the date of any change; and
  112. X
  113. X    b) cause the whole of any work that you distribute or publish, that in
  114. X    whole or in part contains or is a derivative of the APM Library or any
  115. X    part thereof, to be licensed to all third parties on terms identical
  116. X    to those contained in this License Agreement (except that you may
  117. X    choose to grant more extensive warranty protection to some or all
  118. X    third parties, at your option).
  119. X
  120. X    c) You may charge a distribution fee for the physical act of
  121. X    transferring a copy, and you may at your option offer warranty
  122. X    protection in exchange for a fee.
  123. X
  124. X    d) You may not charge a license fee for the whole of any work that
  125. X    you distribute or publish, that in whole or in part contains or is
  126. X    a derivative of the APM library or any part thereof, without the
  127. X    express written permission of Lloyd Zusman and Master Byte Software;
  128. X    whether this permission is granted for free or in return for goods
  129. X    services, royalties, or other compensation will be determined
  130. X    solely by Lloyd Zusman and Master Byte Software.
  131. X
  132. XMere aggregation of another unrelated program with this program (or its
  133. Xderivative) on a volume of a storage or distribution medium does not bring
  134. Xthe other program under the scope of these terms.
  135. X
  136. X  3. You may copy and distribute the APM Library (or a portion or
  137. Xderivative of it, under Paragraph 2) in object code or executable form
  138. Xunder all the terms of Paragraphs 1 and 2 above provided that you also
  139. Xdo one of the following:
  140. X
  141. X    a) accompany it with the complete corresponding machine-readable
  142. X    source code, which must be distributed under the terms of
  143. X    Paragraphs 1 and 2 above; or,
  144. X
  145. X    b) accompany it with a written offer, valid for at least three
  146. X    years, to give any third party free (except for a nominal
  147. X    shipping charge) a complete machine-readable copy of the
  148. X    corresponding source code, to be distributed under the terms of
  149. X    Paragraphs 1 and 2 above; or,
  150. X
  151. X    c) accompany it with the information you received as to where the
  152. X    corresponding source code may be obtained.  (This alternative is
  153. X    allowed only for noncommercial distribution and only if you
  154. X    received the program in object code or executable form alone.)
  155. X
  156. XFor an executable file, complete source code means all the source code
  157. Xfor all modules it contains; but, as a special exception, it need not
  158. Xinclude source code for modules which are standard libraries that
  159. Xaccompany the operating system on which the executable file runs.
  160. X
  161. X  4. You may not copy, sublicense, distribute or transfer the APM
  162. XLibrary except as expressly provided under this License Agreement.
  163. XAny attempt otherwise to copy, sublicense, distribute or transfer the
  164. XAPM Library is void and your rights to use the APM Library under this
  165. XLicense agreement shall be automatically terminated.  However, parties
  166. Xwho have received computer software programs from you with this
  167. XLicense Agreement will not have their licenses terminated so long as
  168. Xsuch parties remain in full compliance.
  169. X
  170. X  5. If you wish to incorporate parts of the APM Library into other
  171. Xprograms whose distribution conditions are different, write to Lloyd
  172. XZusman at Master Byte Software.  We have not yet worked out a simple
  173. Xrule that can be stated here, but we will often permit this.  We will
  174. Xbe guided by the goals of (1) preserving the free status of all
  175. Xderivatives of our free software; of (2) promoting the sharing and
  176. Xreuse of software; and of (3) not allowing anyone to profit from the
  177. Xuse of our software without us also having the opportunity to share
  178. Xin these profits.
  179. X
  180. XYour comments and suggestions about our licensing policies and our
  181. Xsoftware are welcome!  Please contact Lloyd Zusman, Master Byte
  182. XSoftware, 127 Wilder Ave., Los Gatos, California 95030, or call
  183. X(408) 395-5693.
  184. X
  185. X               NO WARRANTY
  186. X
  187. X  BECAUSE THE APM LIBRARY IS LICENSED FREE OF CHARGE, WE PROVIDE
  188. XABSOLUTELY NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE
  189. XLAW.  EXCEPT WHEN OTHERWISE STATED IN WRITING, MASTER BYTE SOFTWARE,
  190. XLLOYD ZUSMAN AND/OR OTHER PARTIES PROVIDE THE APM LIBRARY "AS IS"
  191. XWITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  192. XBUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  193. XFITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY
  194. XAND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE THE APM
  195. XLIBRARY PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY
  196. XSERVICING, REPAIR OR CORRECTION.
  197. X
  198. X  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL MASTER BYTE
  199. XSOFTWARE, LLOYD ZUSMAN, AND/OR ANY OTHER PARTY WHO MAY MODIFY AND
  200. XREDISTRIBUTE THE APM LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR
  201. XDAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR OTHER SPECIAL,
  202. XINCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR
  203. XINABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA
  204. XBEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR A
  205. XFAILURE OF THE PROGRAM TO OPERATE WITH PROGRAMS NOT DISTRIBUTED BY
  206. XMASTER BYTE SOFTWARE) THE PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF
  207. XTHE POSSIBILITY OF SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  208. X
  209. X******************************************************************************/
  210. X
  211. X
  212. X/*
  213. X * Memory allocation routines for the APM library.
  214. X *
  215. X * $Log:    memory.c,v $
  216. X * Revision 1.0  88/10/05  12:38:13  ljz
  217. X * Initial release.
  218. X * 
  219. X */
  220. X#ifndef lint
  221. Xstatic char rcsid[] = "$Header: memory.c,v 1.0 88/10/05 12:38:13 ljz Exp $";
  222. X#endif /* ! lint */
  223. X
  224. X#include <stdio.h>
  225. X#include "apm.h"
  226. X#include "apmlocal.h"
  227. X
  228. Xtypedef struct {
  229. X    APM apm;
  230. X    unsigned int flags;
  231. X} APMMEMTAB;
  232. X
  233. X#define MEMAVAIL    1
  234. X
  235. Xstatic APMMEMTAB *APM_memtab = (APMMEMTAB *)NULL;
  236. Xstatic int APM_memnum = 0;
  237. X
  238. X#ifndef MSC
  239. Xextern char *malloc();
  240. Xextern char *realloc();
  241. Xextern void free();
  242. X#endif /* MSC */
  243. X
  244. Xchar *
  245. XAPM_alloc_mem(mem, nelem, elsize)
  246. Xchar *mem;
  247. Xunsigned int nelem;
  248. Xunsigned int elsize;
  249. X{
  250. X    char *cp;
  251. X    unsigned int size;
  252. X
  253. X    apm_errno = APM_OK;
  254. X
  255. X    if (elsize == 0) {
  256. X        elsize = 1;    /* default to 1 for ease of use */
  257. X    }
  258. X
  259. X    if (nelem == 0) {    /* malloc(0) & realloc(MEM, 0) not portable */
  260. X        nelem = 1;
  261. X    }
  262. X
  263. X    size = nelem * elsize;
  264. X    if (size < nelem || size < elsize) {    /* overflow */
  265. X        (void)APM_set_errno(APM_EPARM);
  266. X        return (NULL);
  267. X    }
  268. X
  269. X    if (mem == NULL) {
  270. X        cp = malloc(size);
  271. X    }
  272. X    else {
  273. X        cp = realloc(mem, size);
  274. X    }
  275. X
  276. X    if (cp == NULL) {
  277. X        (void)APM_set_errno(APM_ENOMEM);
  278. X    }
  279. X
  280. X    return (cp);
  281. X}
  282. X
  283. Xint
  284. XAPM_free_mem(mem)
  285. Xchar *mem;
  286. X{
  287. X    if (mem == NULL) {
  288. X        return (APM_set_errno(APM_WNULL));
  289. X    }
  290. X    free(mem);
  291. X
  292. X    return (APM_OK);
  293. X}
  294. X
  295. XAPM
  296. XAPM_do_alloc()
  297. X{
  298. X    APM apm;
  299. X
  300. X    apm_errno = APM_OK;
  301. X
  302. X    apm = (APM)APM_alloc_mem(NULL, 1, sizeof (struct apm_struct));
  303. X    if (apm == (APM)NULL) {
  304. X        APM_set_errno(APM_ENOMEM);
  305. X        return ((APM)NULL);
  306. X    }
  307. X    apm->magic = APM_MAGIC;
  308. X    apm->sign = 1;
  309. X    apm->base = 0;
  310. X    apm->length = 0;
  311. X    apm->dp = 0;
  312. X    apm->alloclen = 0;
  313. X    apm->data = NULL;
  314. X
  315. X    return (apm);
  316. X}
  317. X
  318. XAPM
  319. XAPM_alloc()
  320. X{
  321. X    APM apm;
  322. X    int i;
  323. X    int slot = -1;
  324. X
  325. X    /*
  326. X     * Go through allocation table to see if we have an available
  327. X     * APM value.  If so, set its state to "not available" and then
  328. X     * return it.
  329. X     */
  330. X    for (i = 0; i < APM_memnum; ++i) {
  331. X        if ((APM_memtab[i].flags & MEMAVAIL) != 0) {
  332. X            if (APM_memtab[i].apm != (APM)NULL) {
  333. X                APM_memtab[i].flags = 0;
  334. X                apm = APM_memtab[i].apm;
  335. X                apm->magic = APM_MAGIC;
  336. X                apm->sign = 1;
  337. X                apm->base = 0;
  338. X                apm->length = 0;
  339. X                apm->dp = 0;
  340. X                for (i = 0; i < apm->alloclen; ++i) {
  341. X                    apm->data[i] = 0;
  342. X                }
  343. X                return (apm);
  344. X            }
  345. X        }
  346. X    }
  347. X
  348. X    /*
  349. X     * If we're here, nothing is available.  We now see if there is
  350. X     * an empty slot.
  351. X     */
  352. X    for (i = 0; i < APM_memnum; ++i) {
  353. X        if (APM_memtab[i].apm == (APM)NULL) {
  354. X            slot = i;
  355. X            break;
  356. X        }
  357. X    }
  358. X
  359. X    /*
  360. X     * If we're here, we couldn't find an unused, pre-allocated value.
  361. X     * If there's an available slot, allocate a value; otherwise,
  362. X     * increase the size of the table and create a slot.
  363. X     */
  364. X    if (slot < 0) {
  365. X        APMMEMTAB *mt;
  366. X        int newmem = APM_memnum;
  367. X        slot = newmem;
  368. X        newmem += TABINCR;
  369. X        mt = (APMMEMTAB *)APM_alloc_mem(APM_memtab, newmem,
  370. X                         sizeof (APMMEMTAB));
  371. X        if (mt == (APMMEMTAB *)NULL) {
  372. X            APM_set_errno(APM_ENOMEM);
  373. X            return ((APM)NULL);
  374. X        }
  375. X        APM_memtab = mt;
  376. X        APM_memnum = newmem;
  377. X        for (i = slot; i < APM_memnum; ++i) {
  378. X            APM_memtab[i].flags = 0;
  379. X            APM_memtab[i].apm = (APM)NULL;
  380. X        }
  381. X    }
  382. X
  383. X    /*
  384. X     * We now can allocate the new APM value.
  385. X     */
  386. X    apm = APM_do_alloc();
  387. X    if (apm != (APM)NULL) {
  388. X        APM_memtab[slot].flags = 0;
  389. X        APM_memtab[slot].apm = apm;
  390. X    }
  391. X
  392. X    return (apm);
  393. X}
  394. X
  395. Xint
  396. XAPM_do_free(apm)
  397. XAPM apm;
  398. X{
  399. X    int ercode;
  400. X
  401. X    apm_errno = APM_OK;
  402. X
  403. X    if (apm == (APM)NULL) {
  404. X        return (APM_WNULL);
  405. X    }
  406. X
  407. X    apm->magic = 0L;
  408. X
  409. X    if (apm->data != NULL && apm->alloclen > 0) {
  410. X        ercode = APM_free_mem((char *)apm->data);
  411. X        if (ercode != APM_OK) {
  412. X            return (ercode);
  413. X        }
  414. X    }
  415. X
  416. X    return (APM_free_mem(apm));
  417. X}
  418. X
  419. Xint
  420. XAPM_garbage_collect()
  421. X{
  422. X    int i;
  423. X    int ercode = APM_OK;
  424. X    int code;
  425. X
  426. X    apm_errno = APM_OK;
  427. X
  428. X    for (i = 0; i < APM_memnum; ++i) {
  429. X        if (APM_memtab[i].apm != (APM)NULL) {
  430. X            if ((APM_memtab[i].flags & MEMAVAIL) != 0) {
  431. X                code = APM_do_free(APM_memtab[i].apm);
  432. X                APM_memtab[i].apm = (APM)NULL;
  433. X                APM_memtab[i].flags = 0;
  434. X                if (code != APM_OK) {
  435. X                    ercode = code;
  436. X                }
  437. X            }
  438. X        }
  439. X    }
  440. X
  441. X    return (APM_set_errno(ercode));
  442. X}
  443. X
  444. Xint
  445. XAPM_free(apm)
  446. XAPM apm;
  447. X{
  448. X    int i;
  449. X
  450. X    apm_errno = APM_OK;
  451. X
  452. X    if (apm == (APM)NULL) {
  453. X        return (APM_set_errno(APM_WNULL));
  454. X    }
  455. X
  456. X    for (i = 0; i < APM_memnum; ++i) {
  457. X        if (APM_memtab[i].apm == apm) {
  458. X            if ((APM_memtab[i].flags & MEMAVAIL) == 0) {
  459. X                APM_memtab[i].flags |= MEMAVAIL;
  460. X                return (APM_set_errno(APM_OK));
  461. X            }
  462. X        }
  463. X    }
  464. X
  465. X    return (APM_set_errno(APM_WNOALLOC));
  466. X}
  467. @\END_OF_FILE_memory.c
  468. else
  469.   echo "shar: Will not over write memory.c"
  470. fi
  471. if `test ! -s misc.c`
  472. then
  473. echo "x - misc.c"
  474. sed 's/^X//' > misc.c << '@\END_OF_FILE_misc.c'
  475. X/******************************************************************************
  476. X
  477. X    Arbitrary Precision Math Library General Public License
  478. X            (Written October 5, 1988)
  479. X
  480. X Copyright (C) 1988 Lloyd Zusman, Master Byte Software, Los
  481. X Gatos, California.  Everyone is permitted to copy and distribute
  482. X verbatim copies of this license, but changing it is not allowed.
  483. X You can also use this wording to make the terms for other programs.
  484. X
  485. X The wording of this license is based on that of the
  486. X "GNU EMACS GENERAL PUBLIC LICENSE" by Richard Stallman,
  487. X Copyright (C) 1985, 1987, 1988, version of February 11, 1988,
  488. X but since some of the text has been changed, please be sure to
  489. X READ THIS CAREFULLY!
  490. X
  491. X  This general public license is intended to give everyone the right
  492. Xto share the Arbitrary Precision Math Library (hereinafter referred to
  493. Xas the "APM Library").  To make sure that you get the rights we want
  494. Xyou to have, I need to make restrictions that forbid anyone to deny
  495. Xyou these rights or to ask you to surrender the rights.
  496. X
  497. X  Specifically, we want to make sure that you have the right to give
  498. Xaway copies of the APM Library, that you receive source code or else
  499. Xcan get it if you want it, that you can change the APM Library or use
  500. Xpieces of it in new programs, and that you know you can do these
  501. Xthings.
  502. X
  503. X  To make sure that everyone has such rights, we have to forbid you to
  504. Xdeprive anyone else of these rights.  For example, if you distribute
  505. Xcopies of the APM Library, you must give the recipients all the
  506. Xrights that you have.  You must make sure that they, too, receive or
  507. Xcan get the source code.  And you must tell them their rights.
  508. X
  509. X  Also, for our own protection, we must make certain that everyone
  510. Xfinds out that there is no warranty for the APM Library.  If the APM
  511. XLibrary is modified by someone else and passed on, we want its
  512. Xrecipients to know that what they have is not what we distributed, so
  513. Xthat any problems introduced by others will not reflect on our
  514. Xreputation.
  515. X
  516. X  Therefore we (Lloyd Zusman and Master Byte Software) make the
  517. Xfollowing terms which say what you must do to be allowed to
  518. Xdistribute or change the APM Library.
  519. X
  520. X            COPYING POLICIES
  521. X
  522. X1. You may copy and distribute verbatim copies of the APM Library
  523. Xsource code as you receive it, in any medium, provided that you
  524. Xconspicuously and appropriately publish on each copy a valid copyright
  525. Xnotice "Copyright (C) 1988 Lloyd Zusman, Master Byte Software, Los
  526. XGatos, California" (or with whatever year is appropriate); keep intact
  527. Xthe notices on all files that refer to this License Agreement and to
  528. Xthe absence of any warranty; and give any other recipients of the the
  529. XAPM Library program a copy of this License Agreement along with the
  530. Xprogram.  You may charge a distribution fee for the physical act of
  531. Xtransferring a copy.
  532. X
  533. X  2. You may modify your copy or copies of the APM Library source code or
  534. Xany portion of it, and copy and distribute such modifications under
  535. Xthe terms of Paragraph 1 above, provided that you also do the following:
  536. X
  537. X    a) cause the modified files to carry prominent notices stating
  538. X    that you changed the files and the date of any change; and
  539. X
  540. X    b) cause the whole of any work that you distribute or publish, that in
  541. X    whole or in part contains or is a derivative of the APM Library or any
  542. X    part thereof, to be licensed to all third parties on terms identical
  543. X    to those contained in this License Agreement (except that you may
  544. X    choose to grant more extensive warranty protection to some or all
  545. X    third parties, at your option).
  546. X
  547. X    c) You may charge a distribution fee for the physical act of
  548. X    transferring a copy, and you may at your option offer warranty
  549. X    protection in exchange for a fee.
  550. X
  551. X    d) You may not charge a license fee for the whole of any work that
  552. X    you distribute or publish, that in whole or in part contains or is
  553. X    a derivative of the APM library or any part thereof, without the
  554. X    express written permission of Lloyd Zusman and Master Byte Software;
  555. X    whether this permission is granted for free or in return for goods
  556. X    services, royalties, or other compensation will be determined
  557. X    solely by Lloyd Zusman and Master Byte Software.
  558. X
  559. XMere aggregation of another unrelated program with this program (or its
  560. Xderivative) on a volume of a storage or distribution medium does not bring
  561. Xthe other program under the scope of these terms.
  562. X
  563. X  3. You may copy and distribute the APM Library (or a portion or
  564. Xderivative of it, under Paragraph 2) in object code or executable form
  565. Xunder all the terms of Paragraphs 1 and 2 above provided that you also
  566. Xdo one of the following:
  567. X
  568. X    a) accompany it with the complete corresponding machine-readable
  569. X    source code, which must be distributed under the terms of
  570. X    Paragraphs 1 and 2 above; or,
  571. X
  572. X    b) accompany it with a written offer, valid for at least three
  573. X    years, to give any third party free (except for a nominal
  574. X    shipping charge) a complete machine-readable copy of the
  575. X    corresponding source code, to be distributed under the terms of
  576. X    Paragraphs 1 and 2 above; or,
  577. X
  578. X    c) accompany it with the information you received as to where the
  579. X    corresponding source code may be obtained.  (This alternative is
  580. X    allowed only for noncommercial distribution and only if you
  581. X    received the program in object code or executable form alone.)
  582. X
  583. XFor an executable file, complete source code means all the source code
  584. Xfor all modules it contains; but, as a special exception, it need not
  585. Xinclude source code for modules which are standard libraries that
  586. Xaccompany the operating system on which the executable file runs.
  587. X
  588. X  4. You may not copy, sublicense, distribute or transfer the APM
  589. XLibrary except as expressly provided under this License Agreement.
  590. XAny attempt otherwise to copy, sublicense, distribute or transfer the
  591. XAPM Library is void and your rights to use the APM Library under this
  592. XLicense agreement shall be automatically terminated.  However, parties
  593. Xwho have received computer software programs from you with this
  594. XLicense Agreement will not have their licenses terminated so long as
  595. Xsuch parties remain in full compliance.
  596. X
  597. X  5. If you wish to incorporate parts of the APM Library into other
  598. Xprograms whose distribution conditions are different, write to Lloyd
  599. XZusman at Master Byte Software.  We have not yet worked out a simple
  600. Xrule that can be stated here, but we will often permit this.  We will
  601. Xbe guided by the goals of (1) preserving the free status of all
  602. Xderivatives of our free software; of (2) promoting the sharing and
  603. Xreuse of software; and of (3) not allowing anyone to profit from the
  604. Xuse of our software without us also having the opportunity to share
  605. Xin these profits.
  606. X
  607. XYour comments and suggestions about our licensing policies and our
  608. Xsoftware are welcome!  Please contact Lloyd Zusman, Master Byte
  609. XSoftware, 127 Wilder Ave., Los Gatos, California 95030, or call
  610. X(408) 395-5693.
  611. X
  612. X               NO WARRANTY
  613. X
  614. X  BECAUSE THE APM LIBRARY IS LICENSED FREE OF CHARGE, WE PROVIDE
  615. XABSOLUTELY NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE
  616. XLAW.  EXCEPT WHEN OTHERWISE STATED IN WRITING, MASTER BYTE SOFTWARE,
  617. XLLOYD ZUSMAN AND/OR OTHER PARTIES PROVIDE THE APM LIBRARY "AS IS"
  618. XWITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  619. XBUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  620. XFITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY
  621. XAND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE THE APM
  622. XLIBRARY PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY
  623. XSERVICING, REPAIR OR CORRECTION.
  624. X
  625. X  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL MASTER BYTE
  626. XSOFTWARE, LLOYD ZUSMAN, AND/OR ANY OTHER PARTY WHO MAY MODIFY AND
  627. XREDISTRIBUTE THE APM LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR
  628. XDAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR OTHER SPECIAL,
  629. XINCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR
  630. XINABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA
  631. XBEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR A
  632. XFAILURE OF THE PROGRAM TO OPERATE WITH PROGRAMS NOT DISTRIBUTED BY
  633. XMASTER BYTE SOFTWARE) THE PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF
  634. XTHE POSSIBILITY OF SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  635. X
  636. X******************************************************************************/
  637. X
  638. X
  639. X/*
  640. X * Miscellaneous routines for the APM library.
  641. X *
  642. X * $Log:    misc.c,v $
  643. X * Revision 1.0  88/10/05  12:38:14  ljz
  644. X * Initial release.
  645. X * 
  646. X */
  647. X#ifndef lint
  648. Xstatic char rcsid[] = "$Header: misc.c,v 1.0 88/10/05 12:38:14 ljz Exp $";
  649. X#endif /* ! lint */
  650. X
  651. X#include <stdio.h>
  652. X#include "apm.h"
  653. X#include "apmlocal.h"
  654. X
  655. Xstatic FILE *APM_debug_file = (FILE *)NULL;
  656. X
  657. XAPM
  658. Xapm_init(init, scaleFactor, base)
  659. Xlong init;
  660. Xint scaleFactor;
  661. Xshort base;
  662. X{
  663. X    APM apm;
  664. X    int ercode;
  665. X
  666. X    apm_errno = APM_OK;
  667. X
  668. X    apm = APM_alloc();
  669. X    if (apm == (APM)NULL) {
  670. X        APM_error(APM_ENOMEM);
  671. X        return ((APM)NULL);
  672. X    }
  673. X
  674. X    ercode = APM_parse_long(apm, init, base);
  675. X    if (ercode < APM_OK) {
  676. X        return ((APM)NULL);
  677. X    }
  678. X    ercode = APM_shift(apm, scaleFactor);
  679. X    if (ercode < APM_OK) {
  680. X        return ((APM)NULL);
  681. X    }
  682. X    return (apm);
  683. X}
  684. X
  685. Xint
  686. Xapm_dispose(apm)
  687. XAPM apm;
  688. X{
  689. X    apm_errno = APM_OK;
  690. X
  691. X    return (APM_error(APM_free(apm)));
  692. X}
  693. X
  694. Xint
  695. Xapm_garbage_collect()
  696. X{
  697. X    apm_errno = APM_OK;
  698. X
  699. X    return (APM_error(APM_garbage_collect()));
  700. X}
  701. X
  702. Xint
  703. Xapm_assign(result, value)
  704. XAPM result;
  705. XAPM value;
  706. X{
  707. X    apm_errno = APM_OK;
  708. X
  709. X    ERR_RETURN(apm_validate(value));
  710. X
  711. X    if (result == value) {
  712. X        return (APM_OK);
  713. X    }
  714. X
  715. X    ERR_RETURN(APM_size(result, value->length));
  716. X
  717. X    APM_copy_bytes(result->data, value->data, value->length * sizeof(short));
  718. X    result->sign = SIGNOF(value->sign);
  719. X    result->base = value->base;
  720. X    result->length = value->length;
  721. X    result->dp = value->dp;
  722. X    return (APM_OK);
  723. X}
  724. X
  725. Xint
  726. Xapm_assign_long(result, longval, scaleFactor, base)
  727. XAPM result;
  728. Xlong longval;
  729. Xint scaleFactor;
  730. Xshort base;
  731. X{
  732. X    static APM localApm = (APM)NULL;
  733. X
  734. X    apm_errno = APM_OK;
  735. X
  736. X    if (localApm == (APM)NULL) {
  737. X        localApm = APM_alloc();
  738. X        if (apm_errno < 0) {
  739. X            return (APM_error(APM_ENOMEM));
  740. X        }
  741. X    }
  742. X    ERR_RETURN(APM_parse_long(localApm, longval, base));
  743. X    ERR_RETURN(APM_shift(localApm, scaleFactor));
  744. X
  745. X    return (APM_error(apm_assign(result, localApm)));
  746. X}
  747. X
  748. Xint
  749. Xapm_assign_string(result, string, base)
  750. XAPM result;
  751. Xchar *string;
  752. Xshort base;
  753. X{
  754. X    apm_errno = APM_OK;
  755. X
  756. X    return (APM_error(APM_parse_string(result, string, base)));
  757. X}
  758. X
  759. Xint
  760. Xapm_round(result, apm, dp)
  761. XAPM result;
  762. XAPM apm;
  763. Xint dp;
  764. X{
  765. X    static APM localApm1 = (APM)NULL;
  766. X    static APM localApm2 = (APM)NULL;
  767. X
  768. X    apm_errno = APM_OK;
  769. X
  770. X    if (dp < 0) {
  771. X        return (APM_set_errno(APM_EPARM));
  772. X    }
  773. X
  774. X    ERR_RETURN(APM_val_format(result));
  775. X    ERR_RETURN(APM_val_format(apm));
  776. X    ERR_RETURN(APM_val_base(apm->base));
  777. X
  778. X    ERR_RETURN(APM_trim(apm, 1, 0));
  779. X
  780. X    if (dp < 0 || dp >= apm->dp) {
  781. X        return (apm_assign(result, apm));
  782. X    }
  783. X
  784. X    if (localApm1 == (APM)NULL) {
  785. X        localApm1 = APM_alloc();
  786. X        if (localApm1 == (APM)NULL) {
  787. X            return (APM_error(APM_ENOMEM));
  788. X        }
  789. X    }
  790. X
  791. X    if (localApm2 == (APM)NULL) {
  792. X        localApm2 = APM_alloc();
  793. X        if (localApm2 == (APM)NULL) {
  794. X            return (APM_error(APM_ENOMEM));
  795. X        }
  796. X    }
  797. X
  798. X    if (apm->base == SPECIAL_BASE) {
  799. X        ERR_RETURN(apm_assign_long(localApm1, 5L, -dp - 1, 10));
  800. X        ERR_RETURN(apm_assign(localApm2, apm));
  801. X        ERR_RETURN(APM_spec_to_norm(localApm2));
  802. X        ERR_RETURN(apm_add(result, localApm2, localApm1));
  803. X        ERR_RETURN(APM_setdp(localApm2, result, dp));
  804. X        ERR_RETURN(apm_assign(result, localApm2));
  805. X        ERR_RETURN(APM_norm_to_spec(result));
  806. X    }
  807. X    else {    
  808. X        ERR_RETURN(apm_assign_long(localApm1, (long)(apm->base / 2),
  809. X                       -dp - 1, apm->base));
  810. X        ERR_RETURN(apm_add(localApm2, apm, localApm1));
  811. X        ERR_RETURN(APM_setdp(result, localApm2, dp));
  812. X    }
  813. X
  814. X    return (APM_set_errno(APM_trim(result, 1, 0)));
  815. X}
  816. X
  817. Xint
  818. Xapm_convert(result, length, dp, round, leftjust, apm)
  819. Xchar *result;
  820. Xint length;
  821. Xint dp;
  822. Xint round;
  823. Xint leftjust;
  824. XAPM apm;
  825. X{
  826. X    static APM localApm1 = (APM)NULL;
  827. X    static APM localApm2 = (APM)NULL;
  828. X    static APM recursiveApm = (APM)NULL;
  829. X    static char *localString = NULL;
  830. X    static int localLen = 0;
  831. X    int roffset;
  832. X    int loffset;
  833. X    int stringlen;
  834. X    int dpos = -1;
  835. X    int n;
  836. X    char *string;
  837. X
  838. X    apm_errno = APM_OK;
  839. X
  840. X    if (result == NULL) {
  841. X        return (APM_error(APM_ENULLVAL));
  842. X    }
  843. X    *result = '\0';
  844. X
  845. X    ERR_RETURN(APM_val_format(apm));
  846. X    ERR_RETURN(APM_val_base(apm->base));
  847. X
  848. X    if (apm->data == NULL && apm->length > 0) {
  849. X        return (APM_error(APM_ENULLVAL));
  850. X    }
  851. X    if (length <= 1) {
  852. X        return (APM_error(APM_EPARM));
  853. X    }
  854. X
  855. X    if (apm->length < 0) {
  856. X        return (APM_error(APM_EPARM));
  857. X    }
  858. X
  859. X    if (apm->base == SPECIAL_BASE) {
  860. X        if (recursiveApm == (APM)NULL) {
  861. X            recursiveApm = APM_alloc();
  862. X            if (recursiveApm == (APM)NULL) {
  863. X                return (APM_error(APM_ENOMEM));
  864. X            }
  865. X        }
  866. X        ERR_RETURN(apm_assign(recursiveApm, apm));
  867. X        ERR_RETURN(APM_spec_to_norm(recursiveApm));
  868. X        return (apm_convert(result, length, dp, round, leftjust,
  869. X                    recursiveApm));
  870. X    }
  871. X
  872. X    if (localApm1 == (APM)NULL) {
  873. X        localApm1 = APM_alloc();
  874. X        if (localApm1 == (APM)NULL) {
  875. X            return (APM_error(APM_ENOMEM));
  876. X        }
  877. X    }
  878. X
  879. X
  880. X    if (!round || dp < 0) {
  881. X        ERR_RETURN(APM_setdp(localApm1, apm, dp));
  882. X    }
  883. X    else {
  884. X        if (localApm2 == (APM)NULL) {
  885. X            localApm2 = APM_alloc();
  886. X            if (localApm2 == (APM)NULL) {
  887. X                return (APM_error(APM_ENOMEM));
  888. X            }
  889. X        }
  890. X        ERR_RETURN(apm_round(localApm2, apm, dp));
  891. X        ERR_RETURN(APM_setdp(localApm1, localApm2, dp));
  892. X    }
  893. X
  894. X    ERR_RETURN(APM_trim(localApm1, 1, 0));
  895. X
  896. X    if (localApm1->dp < 0) {
  897. X        stringlen = localApm1->length - localApm1->dp;
  898. X    }
  899. X    else if (localApm1->length < localApm1->dp) {
  900. X        stringlen = localApm1->dp + 2;
  901. X    }
  902. X    else {
  903. X        stringlen = localApm1->length;
  904. X        if (localApm1->dp > 0) {
  905. X            dpos = localApm1->length - localApm1->dp;
  906. X            ++stringlen;
  907. X            if (dpos == 0) {
  908. X                ++stringlen;
  909. X            }
  910. X        }
  911. X    }
  912. X    if (localApm1->sign < 0) {
  913. X        ++stringlen;
  914. X    }
  915. X    if (stringlen + 1 > localLen) {
  916. X        localString = APM_alloc_mem(localString, stringlen + 1,
  917. X                     sizeof (char));
  918. X        if (localString == NULL) {
  919. X            return (APM_error(APM_ENOMEM));
  920. X        }
  921. X        localLen = stringlen + 1;
  922. X    }
  923. X
  924. X    string = localString;
  925. X    if (localApm1->sign < 0) {
  926. X        *string++ = '-';
  927. X    }
  928. X    if (localApm1->length < localApm1->dp) {
  929. X        *string++ = '0';
  930. X        *string++ = '.';
  931. X        for (n = localApm1->length; n < localApm1->dp; ++n) {
  932. X            *string++ = '0';
  933. X        }
  934. X    }
  935. X    if (dpos == 0) {
  936. X        *string++ = '0';
  937. X    }
  938. X    string = APM_build_string(string, localApm1->data, localApm1->length,
  939. X                   dpos);
  940. X    for (n = localApm1->dp; n < 0; ++n) {
  941. X        *string++ = '0';
  942. X    }
  943. X    *string = '\0';
  944. X
  945. X    if (localString[0] == '\0') {
  946. X        localString[0] = '0';
  947. X        localString[1] = '\0';
  948. X        stringlen = 1;
  949. X    }
  950. X
  951. X    --length;
  952. X    if (leftjust) {
  953. X        roffset = 0;
  954. X        loffset = 0;
  955. X        if (length > stringlen) {
  956. X            length = stringlen;
  957. X        }
  958. X    }
  959. X    else if (stringlen >= length) {
  960. X        roffset = 0;
  961. X        loffset = stringlen - length;
  962. X    }
  963. X    else {
  964. X        roffset = length - stringlen;
  965. X        loffset = 0;
  966. X    }
  967. X
  968. X    APM_copy_bytes(result + roffset, localString + loffset, length);
  969. X    result[length] = '\0';
  970. X    while (roffset-- > 0) {
  971. X        result[roffset] = ' ';
  972. X    }
  973. X
  974. X    if (length < stringlen) {
  975. X        return (APM_error(APM_WTRUNC));
  976. X    }
  977. X    else {
  978. X        return (APM_OK);
  979. X    }
  980. X}
  981. X
  982. Xint
  983. Xapm_scale(result, num, scaleFactor)
  984. XAPM result;
  985. XAPM num;
  986. Xint scaleFactor;
  987. X{
  988. X    int ercode;
  989. X
  990. X    apm_errno = APM_OK;
  991. X
  992. X    ERR_RETURN(APM_val_format(result));
  993. X    ERR_RETURN(apm_assign(result, num));
  994. X
  995. X    ercode = APM_shift(result, scaleFactor);
  996. X    return (APM_error(ercode));
  997. X}
  998. X
  999. Xint
  1000. Xapm_compare(num1, num2)
  1001. XAPM num1;
  1002. XAPM num2;
  1003. X{
  1004. X    static APM localApm = (APM)NULL;
  1005. X    int n;
  1006. X
  1007. X    apm_errno = APM_OK;
  1008. X
  1009. X    ERR_RETURN(apm_validate(num1));
  1010. X    ERR_RETURN(apm_validate(num2));
  1011. X
  1012. X    if (num1 == num2) {
  1013. X        return (0);
  1014. X    }
  1015. X
  1016. X    if (num1->base != num2->base) {
  1017. X        return (APM_error(APM_EBASE));
  1018. X    }
  1019. X
  1020. X    if (localApm == (APM)NULL) {
  1021. X        localApm = APM_alloc();
  1022. X        if (localApm == (APM)NULL) {
  1023. X            return (APM_error(APM_ENOMEM));
  1024. X        }
  1025. X    }
  1026. X
  1027. X    ERR_RETURN(apm_subtract(localApm, num1, num2));
  1028. X
  1029. X    for (n = 0; n < localApm->length; ++n) {
  1030. X        if (localApm->data[n] != 0) {
  1031. X            break;
  1032. X        }
  1033. X    }
  1034. X
  1035. X    if (n >= localApm->length) {
  1036. X        return (0);
  1037. X    }
  1038. X    else if (localApm->sign >= 0) {
  1039. X        return (1);
  1040. X    }
  1041. X    else {
  1042. X        return (-1);
  1043. X    }
  1044. X}
  1045. X
  1046. Xint
  1047. Xapm_compare_long(apm, longval, scaleFactor, base)
  1048. XAPM apm;
  1049. Xlong longval;
  1050. Xint scaleFactor;
  1051. Xshort base;
  1052. X{
  1053. X    static APM localApm = (APM)NULL;
  1054. X    int comparison;
  1055. X
  1056. X    apm_errno = APM_OK;
  1057. X
  1058. X    if (localApm == (APM)NULL) {
  1059. X        localApm = APM_alloc();
  1060. X        if (localApm == (APM)NULL) {
  1061. X            return (APM_error(APM_ENOMEM));
  1062. X        }
  1063. X    }
  1064. X
  1065. X    ERR_RETURN(apm_validate(apm));
  1066. X    ERR_RETURN(APM_parse_long(localApm, longval, base));
  1067. X    ERR_RETURN(APM_shift(localApm, scaleFactor));
  1068. X
  1069. X    comparison = apm_compare(apm, localApm);
  1070. X    if (apm_errno < 0) {
  1071. X        return (APM_error(apm_errno));
  1072. X    }
  1073. X    return (comparison);
  1074. X}
  1075. X
  1076. Xint
  1077. Xapm_sign(apm)
  1078. XAPM apm;
  1079. X{
  1080. X    apm_errno = APM_OK;
  1081. X
  1082. X    ERR_RETURN(apm_validate(apm));
  1083. X
  1084. X    return ((int)SIGNOF(apm->sign));
  1085. X}
  1086. X
  1087. Xint
  1088. Xapm_absolute_value(result, apm)
  1089. XAPM result;
  1090. XAPM apm;
  1091. X{
  1092. X    int sign;
  1093. X
  1094. X    apm_errno = APM_OK;
  1095. X
  1096. X    ERR_RETURN(APM_val_format(result));
  1097. X
  1098. X    if (result == apm) {
  1099. X        return (APM_error(APM_EOVERLAP));
  1100. X    }
  1101. X
  1102. X    sign = apm_sign(apm);
  1103. X    if (apm_errno < 0) {
  1104. X        return (APM_error(apm_errno));
  1105. X    }
  1106. X
  1107. X    if (sign < 0) {
  1108. X        return (APM_error(apm_negate(result, apm)));
  1109. X    }
  1110. X    else {
  1111. X        return (APM_error(apm_assign(result, apm)));
  1112. X    }
  1113. X}
  1114. X
  1115. Xint
  1116. Xapm_negate(result, num)
  1117. XAPM result;
  1118. XAPM num;
  1119. X{
  1120. X    apm_errno = APM_OK;
  1121. X
  1122. X    ERR_RETURN(APM_val_format(result));
  1123. X
  1124. X    if (result == num) {
  1125. X        return (APM_error(APM_EOVERLAP));
  1126. X    }
  1127. X    ERR_RETURN(apm_assign(result, num));
  1128. X
  1129. X    result->sign = -(SIGNOF(result->sign));
  1130. X
  1131. X    return (APM_OK);
  1132. X}
  1133. X
  1134. Xint
  1135. Xapm_reciprocal(result, max_precision, num)
  1136. XAPM result;
  1137. Xint max_precision;
  1138. XAPM num;
  1139. X{
  1140. X    static APM apmOne = (APM)NULL;
  1141. X
  1142. X    apm_errno = APM_OK;
  1143. X
  1144. X    ERR_RETURN(APM_val_format(result));
  1145. X    ERR_RETURN(apm_validate(num));
  1146. X
  1147. X    if (result == num) {
  1148. X        return (APM_error(APM_EOVERLAP));
  1149. X    }
  1150. X
  1151. X    if (apmOne == (APM)NULL) {
  1152. X        apmOne = apm_init(1L, 0, 0);
  1153. X        if (apmOne == (APM)NULL) {
  1154. X            return (APM_error(APM_ENOMEM));
  1155. X        }
  1156. X    }
  1157. X
  1158. X    apmOne->base = num->base;
  1159. X    return (apm_divide(result, max_precision, (APM)NULL, apmOne, num));
  1160. X}
  1161. X
  1162. Xint
  1163. X(*apm_error_func(newfunc))()
  1164. Xint (*newfunc)();
  1165. X{
  1166. X    int (*oldfunc)() = APM_error_func;
  1167. X    APM_error_func = newfunc;
  1168. X    return (oldfunc);
  1169. X}
  1170. X
  1171. Xint
  1172. Xapm_validate(apm)
  1173. XAPM apm;
  1174. X{
  1175. X    apm_errno = APM_OK;
  1176. X
  1177. X    ERR_RETURN(APM_val_format(apm));
  1178. X    ERR_RETURN(APM_val_base(apm->base));
  1179. X
  1180. X    return (APM_OK);
  1181. X}
  1182. X
  1183. XFILE *
  1184. Xapm_debug_file(file)
  1185. XFILE *file;
  1186. X{
  1187. X    FILE *old = APM_debug_file;
  1188. X
  1189. X    APM_debug_file = file;
  1190. X    return (old);
  1191. X}
  1192. @\END_OF_FILE_misc.c
  1193. else
  1194.   echo "shar: Will not over write misc.c"
  1195. fi
  1196. echo "Finished archive 3 of 5"
  1197. # to concatenate archives, remove anything after this line
  1198. exit 0
  1199.